package cn.jingyuan.swan.uaa.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;

/*
    参考：
        https://www.cnblogs.com/lywJ/category/1477949.html

        https://echocow.cn/articles/2019/07/14/1563082247386.html

        https://my.oschina.net/giegie

    短信登录参考：
        https://segmentfault.com/a/1190000016583573?utm_source=tag-newest

    多终端登录状态同步：
        https://msd.misuland.com/pd/3617186409758065020
 */


@Slf4j
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    UserDetailsService userDetailsService;

    @Resource
    PasswordEncoder passwordEncoder;

    // 不定义没有 password grant_type
    @Bean("authenticationManager")
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
            .antMatchers("/favicon.ico")
            .antMatchers("/resources/**")
            .antMatchers("/webjars/**")
            .antMatchers("/static/**")
            .antMatchers("/druid/**")
        ;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .eraseCredentials(true)
            .userDetailsService(userDetailsService)
            .passwordEncoder(passwordEncoder)
        ;
    }

}
